package com.zhonglz.util.minio;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.messages.Item;

/***
 * 文件Minio工具类
 * @author zhonglz
 * @date 2019年9月19日
 */
public class MinioUtils {

	private static MinioClient minioClient;

	static {
		try {
			minioClient = new MinioClient(MinioProperties.endpoint, //
					MinioProperties.accessKey, MinioProperties.secretKey);
			// 桶不存在就创建
			bucketExists();
		} catch (InvalidEndpointException e) {
			e.printStackTrace();
		} catch (InvalidPortException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static MinioClient getClient() {
		return minioClient;
	}

	// 通过文件路径上传文件
	public static void putObject(String objectName, String filePath) throws Exception {
		minioClient.putObject(MinioProperties.bucketName, objectName, filePath);
	}

	// 通过文件流分片上传文件
	public static void putObject(String objectName, InputStream stream) throws Exception {
		int size = stream.available();
		minioClient.putObject(MinioProperties.bucketName, objectName, stream, size, "application/octet-stream");
	}

	// 通过文件和文件头上传文件
	public static void putObject(String objectName, InputStream stream, Map<String, String> headerMap)
			throws Exception {
		minioClient.putObject(MinioProperties.bucketName, objectName, stream, stream.available(), headerMap);
	}
	
	// 通过文件夹前缀获取文件的列表
	public static Iterable<Result<Item>> listObjects(String bucketName,String prefix) throws Exception{
		return minioClient.listObjects(bucketName, prefix);
	}
	
	// 获取文件的列表
	public static Iterable<Result<Item>> listObjects(String bucketName) throws Exception{
		return minioClient.listObjects(bucketName);
	}
	
	// 获取文件的文件流
	public static InputStream getObject(String objectName) throws Exception {
		return minioClient.getObject(MinioProperties.bucketName,objectName);
	}
	// 获取文件的字节数组
	public static byte[] getObjectBytes(String objectName) throws Exception {
		 InputStream object = minioClient.getObject(MinioProperties.bucketName,objectName);
		 ByteArrayOutputStream output = new ByteArrayOutputStream();
		    byte[] buffer = new byte[4096];
		    int n = 0;
		    while (-1 != (n = object.read(buffer))) {
		        output.write(buffer, 0, n);
		    }
		  return output.toByteArray();
	}
		
	// 获取文件的URL
	public static String getObjectUrl(String objectName) throws Exception {
		return minioClient.getObjectUrl(MinioProperties.bucketName,objectName);
	}

	// 获取文件的URL（带过期时间）
	public static String presignedGetObject(String objectName,Integer expires) throws Exception {
		return minioClient.presignedGetObject(MinioProperties.bucketName,objectName, expires);
	}

	// 删除文件
	public void removeObject(String objectName) throws Exception {
		minioClient.removeObject(MinioProperties.bucketName, objectName);
	}

	// 查看文件的元数据
	public static ObjectStat statObject(String objectName) throws Exception {
		return minioClient.statObject(MinioProperties.bucketName, objectName);
	};

	// 判断桶是否存在
	private static void bucketExists() throws Exception {
		String bucketName = MinioProperties.bucketName;
		boolean bucketExists = minioClient.bucketExists(bucketName);
		if (!bucketExists) {
			minioClient.makeBucket(bucketName);
		}
	}

	private MinioUtils() {
	}

}

